package cn.ejiakanghu.logistics.service.impl;

import cn.com.ejiakanghu.core.exceptions.BizException;
import cn.ejiakanghu.logistics.service.DeliveryService;
import cn.hutool.core.util.StrUtil;
import org.linlinjava.litemall.db.dao.AgedmallLgBaseInfoMapper;
import org.linlinjava.litemall.db.dao.AgedmallLgMissionMapper;
import org.linlinjava.litemall.db.domain.AgedmallLgBaseInfo;
import org.linlinjava.litemall.db.domain.AgedmallLgBusiRecord;
import org.linlinjava.litemall.db.domain.AgedmallLgMission;
import org.linlinjava.litemall.db.util.BaseInfoLogisticsStatusErrorMessageEnum;
import org.linlinjava.litemall.db.util.LgBaseInfoStatConstant;
import org.linlinjava.litemall.db.util.LgMissionIsOverEum;
import org.linlinjava.litemall.db.util.LogisticsMissionTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：sujp
 * @date ：Created in 2021/11/4 14:16
 * @description： 配送相关事件
 * @modified By：
 * @version: 1.0
 */
@Service
public class DeliveryServiceImplImpl extends LogisticBusiBaseServiceImpl implements DeliveryService {
    @Resource
    private LogisticsBaseServiceAbstractImpl logisticsBaseServiceAbstractImpl;
    @Resource
    private AgedmallLgMissionMapper agedmallLgMissionMapper;
    @Resource
    private AgedmallLgBaseInfoMapper agedmallLgBaseInfoMapper;

    /**
     * 开始配送任务
     *
     * @param baseInfoId
     * @return
     */
    @Override
    public List<Integer> createStartDeliverMiss(String orderCode, Integer baseInfoId) {
        List<Integer> missionIds = new ArrayList<>();
        int type = LogisticsMissionTypeEnum.DELIVERY.getCode();
        //用户下单事件
        if (orderCode != null) {
            //批量的插入
            missionIds = logisticsBaseServiceAbstractImpl.insertIntoMissonByOrderCode(orderCode, LgBaseInfoStatConstant.BUY_UN_SEND);
        } else if (baseInfoId != null) {
            //单个的插入
            missionIds = logisticsBaseServiceAbstractImpl.insertIntoOneMission(baseInfoId, LgBaseInfoStatConstant.BUY_UN_SEND);
        }
        return missionIds;
    }

    /**
     * 配送中
     *
     * @param manId
     * @param missionId
     * @param messageType
     * @param receiveType
     * @param execType
     */
    @Override
    public void inDelivery(Integer manId, Integer missionId, Integer messageType, Integer receiveType, Integer execType) {
        logisticsBaseServiceAbstractImpl.logisticsEventInProgress(manId, missionId, messageType, receiveType, execType);
    }

    /**
     * 配送完成
     *
     * @param missionId
     */
    @Override
    public void deliveryCompleted(Integer missionId) {
        logisticsBaseServiceAbstractImpl.deliveryCompletionOperation(missionId, LgBaseInfoStatConstant.CHANGE_SENDING, LgBaseInfoStatConstant.CHANGE_SEND_COMPLETE, BaseInfoLogisticsStatusErrorMessageEnum.NOTIN_DELIVERY_STATE.getMessage());
    }
}
